package cn.ggsaas.base.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyuzhen
 */
@ControllerAdvice
@Slf4j
public class ControllerExceptionHandler {

    @ExceptionHandler(BasicException.class)
    @ResponseBody
    public Map<String, Object> handlerUserNotExistException(BasicException bx) {
        String path = bx.getStackTrace()[0].getClassName() + "." + bx.getStackTrace()[0].getMethodName();
        Map<String, Object> result = new HashMap<>();
        result.put("timestamp", LocalDateTime.now().toString());
        result.put("status", bx.getCode());
        result.put("path", path);
        result.put("msg", bx.getMessage());
        log.error("BasicException::path = [{}]", result);
        return result;
    }

}